using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TMS.Application.Common.Interface;
using TMS.Domain.Entity;
using TMS.Infrastructrue.Persistence;

namespace TMS.Infrastructrue
{
    public static class ApplicationStartup
    {

        public static async void MigrateDatabase(this WebApplication app)
        {
            using var scope = app.Services.CreateScope();

            var services = scope.ServiceProvider;

            try
            {
                var context = services.GetRequiredService<TMSDbContext>();

                context.Database.Migrate();

                var appAdmin = services.GetRequiredService<IRepository<Teachers>>();

                var appAdminAvatar = services.GetRequiredService<IRepository<Avatar>>();

                var appUser = services.GetRequiredService<IRepository<TeaUsers>>();

                var appTerm = services.GetRequiredService<IRepository<Term>>();
                
                var appRole = services.GetRequiredService<IRepository<Role>>();

                // 加入三张默认图片
                if (!appAdminAvatar.Table.Any())
                {
                    List<Avatar> list = new List<Avatar>();

                    for (int i = 1; i <= 3; i++)
                    {
                        var avatar = new Avatar
                        {

                            OriginName = "00" + i +".jpg",
                            CurrentName = "00" + i +".jpg",
                            RelativePath = "StaticFile/00"+ i +".jpg",
                            FileType = "image/jpeg "

                        };

                        list.Add(avatar);
                    }


                    await appAdminAvatar.AddBulkAsync(list);

                }

                // 加入八个学期
                if (!appTerm.Table.Any())
                {
                    List<Term> list = new List<Term>();

                    string [] arr = new string [] {"上学期","下学期"};

                    for (int i = 1; i <= 8; i++)
                    {
                        decimal decimali = i ;

                        decimal Gradei =  decimali/2 ;

                        var term = new Term
                        {

                           TermName= "大" + Math.Ceiling(Gradei) + arr[i%2]

                        };

                        list.Add(term);
                    }


                    await appTerm.AddBulkAsync(list);

                }
                
                // 默认管理员
                if (!appAdmin.Table.Any())
                {
                    
                    var admin = new Teachers{

                        TeacherId="001",

                        TeacherName="Admin",

                    };

                    await appAdmin.AddAsync(admin);

                }
                
                // 默认账户
                if (!appUser.Table.Any())
                {
                    
                    var adminAvatar = appAdminAvatar.Table.ToList();

                    var  avatar = adminAvatar.FirstOrDefault(x=>x.CurrentName=="001.jpg");

                    var avatarid = avatar==null ? new Guid() : avatar.Id ;

                    var user = new TeaUsers{

                        UserId="001",

                        PassWord="113",

                        AvatarId=avatarid,
                    };

                      await appUser.AddAsync(user);

                }

                if (!appRole.Table.Any())
                {
                    var role = new Role{

                        TeacherId="001",
                        IsInstructor=true,
                        IsMaster=true,
                        IsTeacher=true

                    };

                    await appRole.AddAsync(role);


                }



            }
            catch (Exception ex)
            {

                throw new Exception($"An error occurred migrating the DB: {ex.Message}");
            }



        }


    }

}